 package net.ruixin.web.struts2;

 import com.opensymphony.xwork2.ActionSupport;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.interceptor.ServletRequestAware;
 import org.apache.struts2.interceptor.ServletResponseAware;
 import org.apache.struts2.interceptor.SessionAware;

 public class SimpleActionSupport extends ActionSupport
   implements ServletRequestAware, SessionAware, ServletResponseAware
 {
   protected final Log logger;
   protected HttpServletRequest request;
   protected HttpServletResponse response;
   protected Map session;
   protected String scripts;

   public SimpleActionSupport()
   {
     this.logger = LogFactory.getLog(super.getClass());
   }

   protected boolean isPost()
   {
     return "POST".equals(this.request.getMethod());
   }

   protected void registerScript(String script) {
     this.scripts = script;
   }

   protected String render(String text, String contentType)
   {
     try
     {
       HttpServletResponse response = ServletActionContext.getResponse();
       if (response != null) {
         response.setHeader("Pragma", "No-cache");
         response.setHeader("Cache-Control", "no-cache");
         response.setDateHeader("Expires", 0L);
         response.setContentType(contentType);
         response.getWriter().write(text);
       }
     } catch (IOException e) {
       this.logger.error(e.getMessage(), e);
     }
     return null;
   }

   protected String renderText(String text)
   {
     return render(text, "text/plain;charset=UTF-8");
   }

   protected String renderHtml(String html)
   {
     return render(html, "text/html;charset=UTF-8");
   }

   protected String renderJson(String json) {
     return render(json, "text/json;charset=UTF-8");
   }

   protected String renderXML(String xml)
   {
     return render(xml, "text/xml;charset=UTF-8");
   }

   protected void redirect(String url) {
     try {
       this.response.sendRedirect(url);
     } catch (IOException e) {
       e.printStackTrace();
     }
   }

   protected String fetchAttrib(String name)
   {
     return this.request.getParameter(name);
   }

   public void setServletResponse(HttpServletResponse httpServletResponse) {
     this.response = httpServletResponse;
   }

   public void setServletRequest(HttpServletRequest request) {
     this.request = request;
   }

   public void setSession(Map session) {
     this.session = session;
   }

     public Map getSession() {
         return session;
     }

     public String getScripts() {
     return this.scripts;
   }

   public String getMessageTest() {
     return this.session.get("RuixinMessage").toString();
   }

   public void addMessage(String message)
   {
     this.session.put("RuixinMessage", message);
   }
 }

